<?php
/**
 * 描述
 * Created by Paco
 * Date: 2018/10/7 Time: 15:51
 */

namespace App\Http\Request\System;


use App\Http\Request\Store;
use App\Models\System\Config;

class ConfigStore extends Store
{

    private $method;

    public function rules()
    {
        $this->method = strtoupper($this->method());

        $rules = [
            'name' => 'required',
        ];
        if ($this->method == 'POST') {
            $rules = [
                'name' => 'required',
                'value' => 'required',
                "prefix" => 'required',
            ];
        } elseif ($this->method == 'PUT') {
            $rules = [
                'config_id' => 'required|integer',
                'name' => 'required',
                'value' => 'required'
            ];
        }
        return $rules;
    }

    public function after()
    {
        $params = $this->validateData();
        if ($this->method == 'POST') {
            $name = Config::query()->where('name', $params['name'])
                ->where('prefix', $params['prefix'])->value('name');
            if ($name) {
                return $this->throwRespondError(10500);
            }
        }
    }

}